<?php
require_once('setup.php');

if ($handle = opendir('courses/')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($myFile = readdir($handle))) 
    {
        echo "$myFile\n";
    
	$fh = fopen('courses/' . $myFile, 'r');
	
	while(!feof($fh))
	{
	    if(($theData = fgets($fh)) == false)
	    {
	        //echo "wtf";
	        break;
	    }
	    $keywords = explode(" | ", $theData);
	    //echo $keywords[1];
	    //echo $keywords[2];
	    //echo $keywords[3];
	    
	    $deptID = substr($myFile, 0, -4);
//echo $deptID;
//echo strlen($myFile);
	    $num = intval($keywords[1]);
	    $name = $keywords[2];
	    $credits = trim($keywords[3]);
	    $len = strlen($credits);
	    $minCredits = intval($credits{0});
	    $maxCredits = intval($credits{$len-1});
	    
	    $isRange = false;
	    
	    if($len > 1)
	    {
	    	if(!strcasecmp(substr($credits, 2, 2), "to"))
	    	{
	    	    $isRange = true;
	    	}
	    }
	    
	    echo "\n";
	    $query = "INSERT INTO Course VALUES ('$deptID', '$num]', '$name', '$minCredits', '$maxCredits', '$isRange')";
            queryMysql($query);
	}
	
	fclose($fh);
    }
}
?>